package org.geektimes.projects.user.validator;

import org.geektimes.projects.user.domain.User;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/3/11 16:26
 */
public class BeanValidationDemo {


    public static void main(String[] args) {

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        // cache the factory somewhere
        Validator validator = factory.getValidator();

        User user = new User();
        user.setId(-1L);
        user.setPassword("***");
        user.setEmail("xxx");
        user.setPhoneNumber("13345678901");


        Set<ConstraintViolation<User>> violations = validator.validate(user);

        violations.forEach(c -> {
                    System.out.println(c.getMessage());
                }
        );
    }


}
